﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;
using System.Windows.Forms;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.InteropServices;
using EnvDTE80;
using System.IO;

namespace KP_CommonLibrary.DesignTime
{
    public class DesignUtil
    {
        private static string _ConnectionString;
        public static string ConnectionString
        {
            get
            {
                if (string.IsNullOrEmpty(_ConnectionString))
                {
                    try
                    {
                        DTE2 dte = GetCurrentDTE();

                        string file = "";
                        string currentPath = Path.GetDirectoryName(dte.ActiveDocument.FullName);
                        string root = Path.GetPathRoot(currentPath);
                        while (string.IsNullOrEmpty(file))
                        {
                            if (File.Exists(currentPath + "\\App.config"))
                                file = currentPath + "\\App.config";
                            else
                            {
                                if (root.Equals(currentPath))
                                    break;
                                currentPath = Directory.GetParent(currentPath).FullName;
                            }
                        }
                        if (!string.IsNullOrEmpty(file))
                        {
                            ExeConfigurationFileMap map = new ExeConfigurationFileMap();
                            map.ExeConfigFilename = file;
                            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
                            _ConnectionString = config.ConnectionStrings.ConnectionStrings["StrDesignConnection"].ConnectionString;
                        }
                    }
                    catch { }
                }
                return _ConnectionString;
            }
        }
        [DllImport("ole32.dll")]
        private static extern void CreateBindCtx(int reserved, out IBindCtx ppbc);
        [DllImport("ole32.dll")]
        private static extern void GetRunningObjectTable(int reserved, out IRunningObjectTable prot);

        public static DTE2 GetCurrentDTE()
        {


            //rot entry for visual studio running under current process.
            string rotEntry = String.Format("!VisualStudio.DTE.9.0:{0}", System.Diagnostics.Process.GetCurrentProcess().Id);
            IRunningObjectTable rot;
            GetRunningObjectTable(0, out rot);
            IEnumMoniker enumMoniker;
            rot.EnumRunning(out enumMoniker);
            enumMoniker.Reset();
            IntPtr fetched = IntPtr.Zero;
            IMoniker[] moniker = new IMoniker[1];
            while (enumMoniker.Next(1, moniker, fetched) == 0)
            {
                IBindCtx bindCtx;
                CreateBindCtx(0, out bindCtx);
                string displayName;
                moniker[0].GetDisplayName(bindCtx, null, out displayName);
                if (displayName == rotEntry)
                {
                    object comObject;
                    rot.GetObject(moniker[0], out comObject);
                    return (EnvDTE80.DTE2)comObject;
                }
            }
            return null;
        }
    }
}
